// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 * Creator: yanking
 * Create time: 2022-03-01 20:50
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.Greedy;

public class CanPlaceFlowers {
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        // 判断能够在给定的地方按照规则种植n朵花
/*        0 1 0 1 0 0 0
        1 0 1 0 1 0 1
        0 1 0 1 0 1 0  -> 边缘需要防止水桶*/
        int len = flowerbed.length;
        if (len == 1) {
            if (flowerbed[0] == 0) {
                n--;
            }
            return n <= 0;
        }

        if (flowerbed[0] == 0 && flowerbed[1] == 0) {
            flowerbed[0] = 1;
            n--;
        }
        if (flowerbed[len - 1] == 0 && flowerbed[len - 2] == 0) {
            flowerbed[len - 1] = 1;
            n--;
        }
        for (int i = 2; i < len; i++) {
            if (flowerbed[i] == 0 && flowerbed[i - 1] == 0 && flowerbed[i + 1] == 0) {
                flowerbed[i] = 1;
                i++;
                n--;
            }
        }
        return n <= 0;
    }
}
